package remote

import (
	mes "ez/apps/mes/document"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Emp struct {
	Id     int    `json:"id" gorm:"primarykey"`
	Name   string `json:"name" gorm:"size:255;" flag:""`
	Code   string `json:"code" gorm:"size:255;" flag:""`
	IdCard string `json:"idCard" gorm:"size:255;" flag:""`
	IsOn   bool   `json:"isOn" gorm:"" flag:""`
}

func GetEmp() {

	for i := 0; i < 50; i++ {
		req := NewEzRequest()
		resp, e := req.PostMessage("http://192.168.7.3:666/api/hr/admin/Emp/list", ss.M{
			"_where": ss.M{},
			"page":   i + 1,
			"limit":  100,
			//"_d":    1,
		})
		if e != nil {
			println(e)
			return
		}
		msg, e := NewMessageFrom(resp)
		if e != nil {
			println(e)
			return
		}
		if msg == nil {
			println("msg is nil")
			return
		}
		wanted := struct {
			Lists []*Emp `json:"lists"`
		}{}
		e = msg.GetData(&wanted)
		if e != nil {
			println(e.Error())
			return
		}
		crud := mes.NewEmpCrud()
		for _, emp := range wanted.Lists {
			find, err := crud.FindOneBy(ss.M{"sn": emp.Code}, nil)
			if err != nil {
				find.Sn = emp.Code
				find.Name = emp.Name
				find.Card = emp.Code
				err = find.Save()
				if err != nil {
					println(err.Error())
				}
			}
		}
		if len(wanted.Lists) == 0 {
			break
		}
		time.Sleep(2 * time.Second)
	}
}
